<?php
class LoginController extends  Controller{
	public function index(){
//        $smaryt = $this->getSmarty();
//        $this->smarty->assign ( 'pagesets', $this->headerDatas() );
//
//        //如果不是post方式的提交，直接转向
//        if(!CommonBase::isPost()){
//            $this->smarty->display("login.tpl");
//            return;
//        }
//
//        $user_name = $password = $remember;
//        extract ( $_POST, EXTR_IF_EXISTS );
//        require_once SERVICE.DS.'admin'.DS.'UsersService.class.php';
//        $userSerivce = new UsersService($this->getDB());
//        $user_info = $userSerivce->checkPassword($user_name, $password );
//
//        if (!empty($user_info->email)) {
//            $_SESSION['user'] = $user_info;
//            $url = WEBSITE_URL."index";
//            $redirect = "<script language='javascript' type='text/javascript'>";
//            $redirect .= "window.location.href='$url'";
//            $redirect .= "</script>";
//            echo $redirect;
//
//        }else{
//            $this->smarty->assign("errortip",$this->userExsist("用户名或是密码不正确"));
//        }
//        $this->smarty->display("login.tpl");
    }
    public function loginAajx(){
        $user_name = $password = $remember;
        extract ( $_POST, EXTR_IF_EXISTS );
        require_once SERVICE.DS.'admin'.DS.'UsersService.class.php';
        $userSerivce = new UsersService($this->getDB());
        $user_info = $userSerivce->checkPassword($user_name, $password );

        if (!empty($user_info->email)) {
            $_SESSION['user'] = $user_info;
            echo json_encode($user_info);
        }else{
            echo json_encode(array(result=>1));
        }

    }
    public function userExsist($msg){
        return "<div class='alert alert-error'>$msg</div>";
    }
    public function isLogin(){
        if($_SESSION['user'] == ""){
            echo json_encode(array("result"=>false));
            return ;
        }else{
            echo json_encode(array("result"=>true,"user"=>$_SESSION['user']));
            return ;
        }
    }
    public function loginout(){
        $_SESSION['user'] = null;
        $url = WEBSITE_URL."index";
        $redirect = "<script language='javascript' type='text/javascript'>";
        $redirect .= "window.location.href='$url'";
        $redirect .= "</script>";
        echo $redirect;
    }
    public function headerDatas(){
        require_once COMMON . DS  . 'Constant.class.php';
        $js = array(WEBSITE_URL."public/assets/js/jquery.validationEngine-zh.js",WEBSITE_URL."public/assets/js/jquery.validationEngine.js");
        $css = array(WEBSITE_URL."public/assets/css/validationEngine.jquery.css");
        $pagesets = array("seo"=>Constant::$seoarray,"js"=>$js,"css"=>$css,"nav"=>Constant::getNav());

        return $pagesets;
    }
    
    public function qqLogin(){
    	 require_once SERVICE.DS.'admin'.DS.'UsersService.class.php';
        $usersSerivce = new UsersService($this->getDB());
    	$userid = $_POST["openid"];
    	$users = $usersSerivce->getUserInfoByID($userid);
    	$result;
    	if(empty($users->userid)){
    		$users = array(
    		'userid' => $_POST["openid"],
            'login_type' => $_POST["login_type"],
    	    'username' => $_POST["nickname"],
            'birthdate' => $_POST["birthdate"],
            'email' => $_POST["email"]
    		);
    		$result = $usersSerivce->addUser($users);
    	}
    	if($result == "1"){
    		echo json_encode ( "OK" );
    	}
    }
    

}